import re

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from pyquery import PyQuery as pq

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)

wait = WebDriverWait(browser, 10)  # 最长超时时间，默认以秒为单位


def index_page():
    """
    抓取索引页
    """
    try:
        url = 'https://opple.tmall.com/p/rd276894.htm'
        browser.get(url)
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.skin-box-bd .item4line1 .item   ')))
        get_products()
    except TimeoutException:
        index_page()


def get_products():
    """
    提取商品数据
    """
    html = browser.page_source
    doc = pq(html)
    items = doc('.skin-box-bd .item4line1 .item   ').items()
    product_list = []
    for item in items:
        photo_html = item.find('.photo').html()
        img_info = re.findall(r'<img data-ks-lazyload="(.*?)".*?alt="(.*?)"/>', photo_html)[0]
        product = {
            'image': img_info[0],
            'price': item.find('.detail .c-price').text(),
            'href': item.find('.detail .item-name').attr('href'),
            'title': img_info[1],
        }
        product_list.append(product)
        print(product)


def main():
    """
    获取页面商品
    """
    index_page()
    browser.close()


if __name__ == '__main__':
    main()
